package com.zhangbo.study.designpatternlearning.singleton;

public class Singleton {

    private static volatile Singleton instance;
//    private static Singleton instance;

    private Singleton() {
        System.out.println("构建对象");
    }


    public static Singleton getInstance() {
        if(null == instance) {
            //synchronized可以确保内存可见性，volatile可有可无
            synchronized (Singleton.class) {
                if(null == instance) {
                    instance = new Singleton();
                }
                // 不进行二次判断，多线程场景会导致构建多个对象
//                instance = new Singleton();
            }
        }
        return instance;
    }

}
